﻿using System.Collections.Generic;
using System;

namespace Inovout.Gis
{
    public abstract class LayerFactory
    {
        #region CreateLayer

        private static List<LayerFactory> layerFactories = new List<LayerFactory>();
        public static void RegiesteLayerFactory(params LayerFactory[] factories)
        {
            layerFactories.AddRange(factories);
        }
        public static IGeometryLayerInfo CreateLayer(IGisTable gisTable)
        {
            LayerMapping layerMapping = SessionFactoryHolder.Instance.GetConfiguration(gisTable.Name).GetLayerMapping(gisTable.Name);
            if (layerMapping != null)
            {
                return Activator.CreateInstance(layerMapping.LayerType, gisTable) as IGeometryLayerInfo;
            }
            foreach (LayerFactory layerFactory in layerFactories)
            {
                IGeometryLayerInfo layer = layerFactory.Create(gisTable);
                if (layer != null)
                {
                    return layer;
                }
            }
            throw new NotImplementedException(gisTable.Name);
        }
        public static ILayerInfo CreateLayer(ILayerInfo layerInfo)
        {
            foreach (LayerFactory layerFactory in layerFactories)
            {
                ILayerInfo layer = layerFactory.Create(layerInfo);
                if (layer != null)
                {
                    return layer;
                }
            }
            return layerInfo;
        }
        public abstract ILayerInfo Create(ILayerInfo layerInfo);
        public abstract IGeometryLayerInfo Create(IGisTable gisTable);

        #endregion
    }
}
